package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuImageMapper;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrValueMapper;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements SpuInfoService {


    @Autowired
    SpuInfoMapper spuInfoMapper;

    @Autowired
    SpuImageMapper spuImageMapper;

    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;


    /**
     * 保存SPU
     * 1、spu_info
     * 2、spu_image
     * 3、spu_sale_attr、spu_sale_attr_value
     * 先操作spu_sale_attr，保存以下值
     * "baseSaleAttrId":"1",
     * "saleAttrName":"选择颜色",
     * 再操作spu_sale_attr_value
     * spuSaleAttrValueList:[
     * {
     * "baseSaleAttrId":"1",
     * "saleAttrValueName":"黑色"
     * }
     * ]
     * 注意：spu_sale_attr、spu_sale_attr_value有冗余存储项，
     * 需要提前设置好值
     * http://api.gmall.com/admin/product/saveSpuInfo
     */
    //TODO 前端提交三级分类id有bug
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean bigSaveSpuInfo(SpuInfo spuInfo) {

        //1、保存spu_info
        spuInfoMapper.insert(spuInfo);
        Long id = spuInfo.getId();


        //2、保存spu_image
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            for (SpuImage image : spuImageList) {
                image.setSpuId(id);
                spuImageMapper.insert(image);
            }
        }

        //3、保存spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            for (SpuSaleAttr saleAttr : spuSaleAttrList) {
                saleAttr.setSpuId(id);
                spuSaleAttrMapper.insert(saleAttr);

                //4、保存 spu_sale_attr_value
                List<SpuSaleAttrValue> spuSaleAttrValueList = saleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    for (SpuSaleAttrValue attrValue : spuSaleAttrValueList) {
                        attrValue.setSpuId(id);
                        attrValue.setSaleAttrName(saleAttr.getSaleAttrName());
                        spuSaleAttrValueMapper.insert(attrValue);
                    }
                }
            }
        }

        return id>0;
    }



}
